package com.zhuiyun.pm.client.login.service.serviceImp;/**
 * @author syc
 * @date 2023/4/2 18:05
 */

import com.zhuiyun.pm.client.login.entity.ChatRecord;
import com.zhuiyun.pm.client.login.entity.ClientUser;
import com.zhuiyun.pm.client.login.mapper.ChatDao;
import com.zhuiyun.pm.client.login.mapper.UserLoginDao;
import com.zhuiyun.pm.client.login.service.ChatRecordService;
import com.zhuiyun.pm.config.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ChatRecordServiceImpl
 * @Description TODO
 * @Author sgo
 * @Date 2023/4/2 18:05
 **/
@Service
public class ChatRecordServiceImpl implements ChatRecordService {
    @Autowired
    ChatDao chatDao;
    @Autowired
    UserLoginDao userLoginDao;
    @Autowired
    ChatRecordServiceImpl chatRecordService;
    /**
    * @Author sgo
    * @Description  查询聊天记录
    * @Date 2023/4/2 18:09
    * @Return com.zhuiyun.pm.config.CommonResult
    * @param fromUserName
    * @param toUserName
    */
    @Override
    public CommonResult getUserChatRecord(String fromUserName, String toUserName) {
        final Integer integer = chatRecordService.modifyMessagestate(fromUserName, toUserName);
//        Example example11=new Example(ChatRecord.class);
//        example11.createCriteria().andEqualTo("fromUsername", fromUserName).
//                andEqualTo("toUsername", toUserName).orEqualTo("messageStatus", 1);
//        ChatRecord chatRecord=new ChatRecord();
//        chatRecord.setMessageStatus(1);
//        chatDao.updateByPrimaryKey()

        Example example=new Example(ChatRecord.class);
        example.createCriteria().andEqualTo("fromUsername", fromUserName).
                andEqualTo("toUsername", toUserName).orEqualTo("fromUsername", toUserName).andEqualTo("toUsername", fromUserName);
//      聊天记录
        final List<ChatRecord> select = chatDao.selectByExample(example);
        System.out.println("select::"+select);

        ClientUser clientUser=new ClientUser();
        clientUser.setUserName(toUserName);
        final List<ClientUser> clientUser1 = userLoginDao.select(clientUser);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("chatList",select);
        map.put("ChatUser",clientUser1);
        if(select.isEmpty()){
            return CommonResult.error("没聊天记录");
        }else{
            return CommonResult.ok(map);
        }
    }
    /**
    * @Author sgo
    * @Description 添加消息
    * @Date 2023/4/2 19:27 
    * @Return int
    * @param fromUsernmae
    * @param text
    * @param toUsername
    * @param state
    */
    
    @Override
    public int insertChatRecord(String fromUsernmae, String text, String toUsername, String state) {
        ChatRecord chatRecord=new ChatRecord();
        chatRecord.setFromUsername(fromUsernmae);
        chatRecord.setChatContent(text);
        chatRecord.setChatTime(new Date());
        chatRecord.setToUsername(toUsername);
        chatRecord.setMessageStatus(Integer.parseInt(state));
        final int insert = chatDao.insert(chatRecord);
        return insert;
    }
    /**
    * @Author sgo
    * @Description 修改消息状态
    * @Date 2023/4/2 20:46
    * @Return java.lang.Integer
    * @param fromUserName
    * @param toUserName
    */

    @Override
    public Integer modifyMessagestate(String fromUserName, String toUserName) {
        ChatRecord chatRecord=new ChatRecord();
        chatRecord.setFromUsername(toUserName);
        chatRecord.setToUsername(fromUserName);
        List<ChatRecord> chatRecordEntities =chatDao.select(chatRecord);
        for (ChatRecord chatRecordEntity : chatRecordEntities) {
            if (chatRecordEntity.getMessageStatus() == 0) {
                chatRecordEntity.setMessageStatus(1);
                chatDao.updateByPrimaryKeySelective(chatRecordEntity);
            }
        }
        return 1;
    }
}
